﻿<?
@session_start();
require_once("conn.php");

	$cmd = $_REQUEST["cmd"];
	switch($cmd){
		case "frmAddCategory"		:	frmAddCategory();break;
		case "addCategory"			:	addCategory();break;
		case "frmEditCategory"		:	frmEditCategory();break;
		case "editCategory"			:	editCategory();break;
		case "frmAddProduct"		:	frmAddProduct();break;
		case "addProduct"			:	addProduct();break;
		case "frmEditProduct"		:	frmEditProduct();break;
		case "editProduct"			:	editProduct();break;
		case "frmAddUnit"			:	frmAddUnit();break;
		case "addUnit"				:	addUnit();break;
		case "frmEditUnit"			:	frmEditUnit();break;
		case "editUnit"				:	editUnit();break;
		case "addCart"				:	addCart();break;
		case "getUnitcall"			:	getUnitcall();break;
		case "loadProductTable"		:	loadProductTable();break;
		case "frmNewLogoshop"		:	frmNewLogoshop();break;
		case "addNewLogoshop"		:	addNewLogoshop();break;
		case "frmeditLogo"			:	frmeditLogo();break;
		case "frmBuyStock"			:	frmBuyStock();break;	
		case "addBuyStock"			:	addBuyStock();break;
		case "editLogoProduct"		:	editLogoProduct();break;
		case "delStock"				:	delStock();break;	
		case "getPName"				:	getPName();break;								
		case "getAccess"			:	getAccess();break;
		case "updateAccess"			:	updateAccess();break;
		case "updateOrCat"			:	updateOrCat();break;
		case "addSendOffice"		:	addSendOffice();break;
		case "updateSendItem"		:	updateSendItem();break;
		case "addSendIsland"		:	addSendIsland();break;
		case "acceptOrder"			:	acceptOrder();break;
		case "confirmSended"		:	confirmSended();break;
		case "searchProduct"		:	searchProduct();break;
		case "viewAcceptDetail"		:	viewAcceptDetail();break;
		case "addOrderCart"			:	addOrderCart();break;
		case "loadCart"				:	loadCart();break;
		case "removeCart"			:	removeCart();break;
		case "addSendCart"			:	addSendCart();break;
		case "updateSendCart"		:	updateSendCart();break;
		case "removeSendCart"		:	removeSendCart();break;
		case "clearUnfinished"		:	clearUnfinished();break;
		case "delUnfinished"		:	delUnfinished();break;
		case "getOrderForm"			:	getOrderForm();break;
		case "delUser"				:	delUser();break;
		case "newRepair"			:	newRepair();break;
		case "addRepair"			:	addRepair();break;
		case "confirmRepair"		:	confirmRepair();break;
		case "getLastProductIndex"	:	getLastProductIndex();break;
		case "addStockCart"			:	addStockCart();break;
		case "updateStockCart"		:	updateStockCart();break;
		case "removeStockCart"		:	removeStockCart();break;
		case "addStockOffice"		:	addStockOffice();break;
		case "showRemoveStock"		:	showRemoveStock();break;
		case "remStockOfficeCart"	:	remStockOfficeCart();break;
		case "updateRemStockCart"	:	updateRemStockCart();break;
		case "remFromRemStockOfficeList"	:	remFromRemStockOfficeList();break;
		case "remStockOffice"		:	remStockOffice();break;
		case "repairSaveNew"		:	repairSaveNew();break;
		case "finishRepairAdd"		:	finishRepairAdd();break;
		case "delRepairAdd"			:	delRepairAdd();break;
		case "addExport"			:	addExport();break;
		case "finishExport"			:	finishExport();break;
		case "saveEditDealer"		:	saveEditDealer();break;
		case "delDealer"			:	delDealer();break;
		case "saveNewDealer"		:	saveNewDealer();break;

		default						:	echo "Command Error ".$cmd;
										break;
	}

function frmAddCategory(){
?>
	<table>
		<tr>
			<td>Category Name : </td>
			<td><input type="text" id="cat_name"></td>
		</tr>
		<tr>
			<td colspan="2"><input type="button" id="btnAddCategory" onclick="addCategory()" value="Add"></td>
		</tr>
	</table>
<?
}

function addCategory(){
	$cat_name = $_POST["cat_name"];
	$sql = "INSERT INTO pch_category(category_name) VALUES('".$cat_name."');";
	mysql_query($sql) or die(mysql_error());
	echo "Success";
}

function frmEditCategory(){
	$id = $_POST["id"];

	$sql = "SELECT * FROM pch_category WHERE id='$id'";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	?>
	<table>
		<tr>
			<td>Category Name : </td>
			<td><input type="text" id="cat_name" value="<?=$row["category_name"]?>"></td>
		</tr>
		<tr>
			<td colspan="2"><input type="button" onclick="loadCatManage();" value="Back"><input type="button" onclick="editCategory('<?=$row["id"]?>')" value="Edit"></td>
		</tr>
	</table>
<?
}

function editCategory(){
	$id = $_POST["id"];
	$name = $_POST["name"];

	$sql = "UPDATE pch_category SET category_name='".$name."' WHERE id='".$id."';";
	mysql_query($sql) or die(mysql_error());
	echo "Success";
}

function getLastProductIndex(){
	$id = $_POST["id"];
	$sql = "select barcode from pch_product WHERE category_id='".$id."' order by barcode desc LIMIT 0,1;";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	$index = substr($row["barcode"],1);
	echo $index+1;
}

function frmAddProduct(){
?>	
<script>


	function checkSymbol(){
		var tmp = $("#pro_cat").val().split(":");
		
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=getLastProductIndex",
		   data: "&id="+tmp[0]+"&t="+Math.random(),
		   success: function(msg){
			   $("#pro_index").val(msg);
			   strCode = $("#pro_index").val()+tmp[1];
				$("#barcode").val(strCode);
		   }
		});

	}
</script>
 	<table>
		<tr>
			<td>Category : </td>
			<td>
				<select id="pro_cat" onchange="checkSymbol()">
					<option value="">&nbsp;</option>
					<?
						$sql = "SELECT * FROM pch_category";
						$query = mysql_query($sql);
						while($row = mysql_fetch_array($query)){
						?>
							<option value="<?=$row["id"].":".$row["symbol"]?>"><?=$row["category_name"]?></option>
						<?
						}
					?>
				</select>
			</td>
			<tr>
				<td>Image</td>
				<td>
					<input type="hidden" id="pro_pic_edit" value="1">
					<input type="hidden" id="pro_pic">
					<iframe id="uploadtarget" name="uploadtarget" src="" style="width:0px;height:0px;border:0"></iframe>
					<span id="upmsg"></span>
					<span id="frm_pro_pic">
						<form id="frmUpload" action="upload.php" method="post" enctype="multipart/form-data" onsubmit="return clickupload();" target="uploadtarget">
							<input id="fileupload" name="fileupload" type="file">&nbsp;
							<input id="btnUpload" type="submit" value="Upload เลยเด่ะ">
						</form>
					</span>
				</td>
			</tr>
		</tr>
		
		<tr>
			<td>ลำดับสินค้า : </td>
			<td><input type="text" id="pro_index" readonly></td>
		</tr>
		
		<tr>
			<td>ชื่อสินค้า : </td>
			<td><input type="text" id="pro_name_th"></td>
		</tr>
		<tr>
			<td>Product Name (MM) : </td>
			<td><input type="text" id="pro_name_mm"></td>
		</tr>
		<tr>
			<td>Barcode : </td>
			<td><input type="text" id="barcode" readonly></td>
		</tr>
		
		<tr>
			<td colspan="2"><input type="button" id="btnAddProduct" onclick="addProduct()" value="Add"></td>
		</tr>
	</table>
<?
}

function addProduct(){
	$barcode = $_REQUEST["barcode"];
	$nameth = $_REQUEST["nameth"];
	$namemm = $_REQUEST["namemm"];
	$cat = $_REQUEST["cat"];
	$image = $_REQUEST["image"];

	if($image != ""){
		$tmp = explode("/",$image);
		$images = $tmp[1];
		if (@copy($image,"usr/product/".$images)) {
			@unlink($image);
		}
	}else{
		$images = "";
	}

	$sql = "INSERT INTO pch_product(barcode,product_name,category_id,image) VALUES('".$barcode."','".$nameth."',".$cat.",'".$images."');";
//echo $sql;
	mysql_query($sql) or die(mysql_error());
	$lastid = mysql_insert_id();
	$sql = "INSERT INTO pch_product_language(product_id,th,mm) VALUES('".$lastid."','".$nameth."','".$namemm."');";
//echo $sql;
	mysql_query($sql) or die(mysql_error());

	echo "Success";

}

function frmEditProduct(){
	$id = $_POST["id"];
	$sql = "SELECT * FROM pch_product WHERE id='$id'";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
?>
	<table>
		<tr>
			<td>Barcode : </td>
			<td><input type="text" id="barcode" value="<?=$row["barcode"]?>"></td>
		</tr>
		<?
			$ssql = "SELECT * FROM pch_product_language WHERE product_id='".$row["id"]."';";
			$squery = mysql_query($ssql);
			$srow = mysql_fetch_assoc($squery);
		?>
		<tr>
			<td>ชื่อสินค้า : </td>
			<td><input type="text" id="pro_name_th" value="<?=$srow["th"]?>"></td>
		</tr>
		<tr>
			<td>Product Name : </td>
			<td><input type="text" id="pro_name_mm" value="<?=$srow["mm"]?>"></td>
		</tr>
		<tr>
			<td>Category : </td>
			<td>
				<select id="pro_cat">
					<option value="">&nbsp;</option>
					<?
						$sqls = "SELECT * FROM pch_category ORDER BY category_name";
						$querys = mysql_query($sqls);
						while($rows = mysql_fetch_array($querys)){
							if($row["category_id"] == $rows["id"]){
								$selected = "selected";
							}else{
								$selected = "";
							}
						?>
							<option value="<?=$rows["id"]?>" <?=$selected?>><?=$rows["category_name"]?></option>
						<?
						}
					?>
				</select>
			</td>
			<tr>
				<td>Unit Type : </td>
				<td>
					<?
						$xsql = "SELECT * FROM pch_product_call WHERE id_product='".$row["id"]."'";
						//echo
						$xqry = mysql_query($xsql);
						$xrow = mysql_fetch_assoc($xqry);
					?>
					<select id="pro_unit">
						<option value="">&nbsp;</option>
						<?
							$sqls = "SELECT * FROM pch_unit ORDER BY unit_name;";
							$querys = mysql_query($sqls);
							while($rows = mysql_fetch_array($querys)){
								if($xrow["id_unit"] == $rows["id"]){
									$selected = "selected";
								}else{
									$selected = "";
								}
							?>
								<option value="<?=$rows["id"]?>" <?=$selected?>><?=$rows["unit_name"]?></option>
							<?
							}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>สถานที่เก็บ : </td>
				<td>
					<?
						$xsql = "SELECT * FROM pch_product_target WHERE product_id='".$row["id"]."'";
						//echo
						$xqry = mysql_query($xsql);
						$xrow = mysql_fetch_assoc($xqry);
					?>
					<select id="pro_target">
						<option value="">&nbsp;</option>
						<?
							$sqls = "SELECT * FROM pch_target;";
							$querys = mysql_query($sqls);
							while($rows = mysql_fetch_array($querys)){
								if($xrow["target_id"] == $rows["id"]){
									$selected = "selected";
								}else{
									$selected = "";
								}
							?>
								<option value="<?=$rows["id"]?>" <?=$selected?>><?=$rows["targetname"]?></option>
							<?
							}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>Image</td>
				<td>
					<input type="hidden" id="pro_pic_edit" value="1">
					<input type="hidden" id="pro_pic" value="<?=$row["image"]?>">
					<iframe id="uploadtarget" name="uploadtarget" src="" style="width:0px;height:0px;border:0"></iframe>
					<span id="upmsg"></span>
					<span id="frm_pro_pic">
						<form id="frmUpload" action="upload.php" method="post" enctype="multipart/form-data" onsubmit="return clickupload();" target="uploadtarget">
							<input id="fileupload" name="fileupload" type="file">&nbsp;
							<input id="btnUpload" type="submit" value="เสร็จแล้วกด Upload เลยคร๊าบ">
						</form>
					</span>
				</td>
			</tr>
		</tr>
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td colspan="2"><span style="float:left;"><input type="button" onclick="closeEditProduct();" value="Back" style="width:100px;"></span><span style="float:right;"><input type="button" id="btnEditProduct" onclick="editProduct('<?=$row["id"]?>')" value="Edit" style="width:100px;"></span></td>
		</tr>
	</table>
<?
}

function editProduct(){
	$id = $_POST["id"];
	$barcode = $_POST["barcode"];
	$nameth = $_POST["nameth"];
	$namemm = $_POST["namemm"];
	$cat = $_POST["cat"];
	$image = $_POST["image"];
	$pic_edit = $_POST["pic_edit"];
	$unit = $_POST["unit"];
	$target = $_REQUEST["target"];
	
	if($pic_edit == "1"){
		$images = $image;
	}else{
		if($image != ""){
			$tmp = explode("/",$image);
			//$xx = count($tmp) -1;
			$images = $tmp[1];
echo $images;
			//$images = str_replace("usr/product/","",$image);
			if (@copy($image,"usr/product/tmp/".$images)) {
				@unlink($image);
			}
		}else{
			$images = "";
		}
	}

	$sql = "UPDATE pch_product SET barcode='".$barcode."',category_id=".$cat.",image='".$images."' WHERE id='".$id."';";
	mysql_query($sql) or die(mysql_error());
	
	$sql = "UPDATE pch_product_language SET th='".$nameth."',mm='".$namemm."' WHERE product_id='".$id."';";
	mysql_query($sql) or die(mysql_error());

	if($target != ""){
		$sql = "SELECT * FROM pch_product_target WHERE product_id='".$id."';";
		$query = mysql_query($sql);
		if(mysql_num_rows($query) > 0){
			$sql = "UPDATE pch_product_target SET target_id='".$target."' WHERE product_id='".$id."';";
			//echo "a>".$sql;
			mysql_query($sql) or die(mysql_error());
		}else{
			$sql = "INSERT INTO pch_product_target(product_id,target_id) VALUES('".$id."','".$target."');";
			//echo "b>".$sql;
			mysql_query($sql) or die(mysql_error());
		}
	}
	
	if($unit != ""){
		$sql = "REPLACE INTO pch_product_call (id_product,id_unit) VALUES ('".$id."','".$unit."');";
		mysql_query($sql) or die(mysql_error());
	}

	echo "Success";
}

function frmAddUnit(){
?>
	<table>
		<tr>
			<td>Unit Name : </td>
			<td><input type="text" id="unit_name"></td>
		</tr>
		<tr>
			<td colspan="2"><input type="button" id="btnAddUnit" onclick="addUnit()" value="Add"></td>
		</tr>
	</table>
<?
}

function addUnit(){
	$unit_name = $_POST["unit_name"];
	$sql = "INSERT INTO pch_unit(unit_name) VALUES('".$unit_name."');";
	mysql_query($sql) or die(mysql_error());
	echo "Success";
}

function frmEditUnit(){
	$id = $_POST["id"];

	$sql = "SELECT * FROM pch_unit WHERE id='$id'";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	?>
	<table>
		<tr>
			<td>Unit Name : </td>
			<td><input type="text" id="unit_name" value="<?=$row["unit_name"]?>"></td>
		</tr>
		<tr>
			<td colspan="2"><input type="button" onclick="loadUnitManage();" value="Back"><input type="button" onclick="editUnit('<?=$row["id"]?>')" value="Edit"></td>
		</tr>
	</table>
<?
}

function editUnit(){
	$id = $_POST["id"];
	$name = $_POST["name"];

	$sql = "UPDATE pch_unit SET unit_name='".$name."' WHERE id='".$id."';";
	mysql_query($sql) or die(mysql_error());
	echo "Success";
}

function addCart(){
	$item = $_POST["item"];
	$amount = $_POST["amount"];
	$unit = $_POST["unit"];
	$quick = $_POST["quick"];

	//print_r($item);
	//print_r($amount);
	//print_r($unit);
	//print_r($quick);

	$last = 0;

	$sql = "INSERT INTO pch_order(order_type,order_date,order_by) VALUES(1,now(),'".$_SESSION["usernames"]."');";
	$query = mysql_query($sql) or die(mysql_error());
	$last = mysql_insert_id();
	//echo $sql;
	for($i=0;$i<count($item);$i++){
		if($item[$i] == "undefined"){

		}else{
			if($i == 0){
				$values .= "";
			}else{
				$values .= ",";
			}
			$values .= "(".$last.",".$item[$i].",'".$amount[$i]."','".$unit[$i]."','".$quick[$i]."',now())";
		}
	}

	$sql = "INSERT INTO pch_order_detail(order_id,product_id,amount,unit,status,last_update) VALUES".$values.";";
	//echo $sql;
	mysql_query($sql) or die(mysql_error());
	
	//$sql = "DELETE FROM pch_tmp_order;";
	//mysql_query($sql) or die(mysql_error());
	echo "orderid:".$last;

}

function getUnitcall(){
	$id = $_POST["id"];

	$sql = "SELECT id_unit,unit_name FROM pch_product_call JOIN pch_unit ON id_unit=id WHERE id_product='$id'";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	echo $row["id_unit"].":".$row["unit_name"];

}

function loadProductTable(){
	$id = $_POST["id"];
	$sql = "SELECT a.id AS ids,category_name,barcode,th,mm,image,unit_name,targetname,targetmm FROM (pch_product AS a right join pch_product_language on a.id = product_id) Inner Join pch_category AS b ON a.category_id = b.id Left Join pch_product_call AS c ON a.id = c.id_product Left Join pch_unit AS d ON c.id_unit = d.id left join pch_product_target as e on a.id=e.product_id left join pch_target as f on e.target_id=f.id WHERE a.id='".$id."';";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	?>
	<table width="100%">
		<tr>
			<td style="width:120px;height:100px;" align="center"><?if($row["image"] != ""){ echo '<a href="'.$row["image"].'" target="_blank"><img src="'.$row["image"].'" width="100" style="border:0px;"></a>';}else{ echo "&nbsp;";}?></td>
			<td>
				<div>Barcode : <?=$row["barcode"]?></div>
				<div>ชื่อสินค้า : <?=$row["th"]?></div>
				<div>Product Name : <?=$row["mm"]?></div>
				<div>Category : <?=$row["category_name"]?></div>
				<div>Unit : <?=$row["unit_name"]?></div>
				<div>สถานที่เก็บ : <?=$row["targetname"]?></div>
				<div>
					<?
						if(checkAccess("4")){
					?>
						<a href="#" onclick="return frmEditProduct('<?=$row["ids"]?>');">Edit</a>
					<?
						}
					?>
				</div>
			</td>
		</tr>				
	</table>
	<?
}


function frmNewLogoshop(){
?>
	<table>
		<tr>
			<td>Barcode : </td>
			<td><input type="text" id="log_code" OnKeyPress="return chkNumber(this)"></td>
		</tr>
		<tr>
			<td>Category : </td>
			<td><select id="log_cat">
				<?
					$sql = "SELECT * FROM category;";
					$query = mysql_query($sql);
					while($row = mysql_fetch_array($query)){
					?>
						<option value="<?=$row["Cat_ID"]?>"><?=$row["Category_Name"]?></option>
					<?
					}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Product Name : </td>
			<td><input type="text" id="log_name"></td>
		</tr>
		<tr>
			<td>Price : </td>
			<td><input type="text" id="log_price" OnKeyPress="return chkNumber(this)"></td>
		</tr>
		<tr>
			<td colspan="2"><input type="button" id="btnnewLogoProduct" onclick="newLogoProduct()" value="Add"></td>
		</tr>
	</table>
<?
}

function addNewLogoshop(){
	$code = $_POST["code"];
	$cat = $_POST["cat"];
	$name = $_POST["name"];
	$price = $_POST["price"];

	$sql = "INSERT INTO stock(Barcode,Category,Product_Name,Qty,Price,Point_Order) VALUES('".$code."',".$cat.",'".$name."',0,".$price.",3);";
	mysql_query($sql) or die(mysql_error());

	$sql = "INSERT INTO pch_product(barcode,product_name,category_id) VALUES('".$code."','".$name."',2);";
	mysql_query($sql) or die(mysql_error());
	$pro_id = mysql_insert_id();

	$sql = "INSERT INTO pch_product_language(product_id,mm,th) VALUES(".$pro_id.",'".$name."','".$name."');";
	mysql_query($sql) or die(mysql_error());
	echo "Success";
}

function frmeditLogo(){
	$barcode = $_REQUEST["barcode"];
	$sql = "SELECT * FROM stock WHERE Barcode='".$barcode."'";
	$qry = mysql_query($sql);
	$rw = mysql_fetch_assoc($qry);
?>
	<table>
		<tr>
			<td>Barcode : </td>
			<td><input type="hidden" id="old_code" value="<?=$rw["Barcode"]?>"><input type="text" id="st_code" value="<?=$rw["Barcode"]?>" OnKeyPress="return chkNumber(this)"></td>
		</tr>
		<tr>
			<td>Category : </td>
			<td><select id="st_cat">
				<?
					$sql = "SELECT * FROM category;";
					$query = mysql_query($sql);
					while($row = mysql_fetch_array($query)){
						if($rw["Category"] == $row["Cat_ID"]){
							$selec = "selected";
						}else{
							$selec = "";
						}
					?>
						<option value="<?=$row["Cat_ID"]?>" <?=$selec?>><?=$row["Category_Name"]?></option>
					<?
					}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Product Name : </td>
			<td><input type="text" id="st_name" value="<?=$rw["Product_Name"]?>"></td>
		</tr>
		<tr>
			<td>Price : </td>
			<td><input type="text" id="st_price" value="<?=$rw["Price"]?>" OnKeyPress="return chkNumber(this)"></td>
		</tr>
		<tr>
			<td colspan="2"><input type="button" id="btneditLogoProduct" onclick="editLogoProduct()" value="Save"></td>
		</tr>
	</table>
<?
}

function frmBuyStock(){
?>
	<script type="text/javascript">
		function findValue(li) {
		if( li == null ) return alert("No match!");

		// if coming from an AJAX call, let's use the CityId as the value
		if( !!li.extra ) var sValue = li.extra[0];

		// otherwise, let's just display the value in the text box
		else var sValue = li.selectValue;

		//alert("The value you selected was: " + sValue);
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return row[0] + " | " + row[1];
		}

		$().ready(function() {
			$("#buyBarcode").autocomplete(
			  "autocomplete.php",
			  {
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:10,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			$("#buyBarcode").focus();
		});
	  
	</script>
	<input type="hidden" id="st_code">
	<input type="hidden" id="st_index" value="1">
	<table>
		<tr>
			<td>Barcode : </td>
			<td><input type="text" style="width: 200px;" value="" id="buyBarcode" class="ac_input"/></td>
			<td>&nbsp;</td>
			<td>Amount : </td>
			<td><input type="text" id="buyAmount"></td>
			<td><input type="button" onclick="addBuyStock()" value="Add"></td>
		</tr>
		<tr>
			<td colspan="6"><input type="button" id="btnStCheckOut" onclick="st_CheckOut()" value="Check Out" disabled></td>
		</tr>
	</table>
	<table id="tblStock" width="430" border="1" style="display:none;">
		<tr>
			<td colspan="4">รายการเพิ่มของลง Logo Shop วันที่  <?=date("d/m/Y");?></td>
		</tr>
		<tr>
			<td width="200">Barcode</td>
			<td width="80" align="center">Amount</td>
			<td width="50">&nbsp;</td>
		</tr>
	</table>
<?
}

function addBuyStock(){
	$item = $_POST["item"];
	$amount = $_POST["amount"];
	
	for($i=0;$i<count($item);$i++){
		if($i == 0){
			$values .= "";
		}else{
			$values .= ",";
		}
		$values .= "('".$item[$i]."','".$amount[$i]."',now(),'".$_SESSION["usernames"]."')";
	}

	$sql = "INSERT INTO buy(Barcode,Qty,Buy_Date,Buy_by) VALUES".$values.";";
	mysql_query($sql) or die(mysql_error());

	for($i=0;$i<count($item);$i++){
		$sql = "UPDATE stock SET Qty = Qty + ".$amount[$i]." WHERE Barcode='".$item[$i]."'";
		mysql_query($sql) or die(mysql_error());		
	}

	echo "orderid:1";
}

function editLogoProduct(){
	$oldCode = $_POST["oldCode"];
	$barcode = $_POST["barcode"];
	$st_cat = $_POST["st_cat"];
	$st_name = $_POST["st_name"];
	$st_price = $_POST["st_price"];

	$sql = "UPDATE stock SET Barcode='".$barcode."',Category=".$st_cat.",Product_Name='".$st_name."',Price=".$st_price." WHERE Barcode='".$oldCode."'";
	mysql_query($sql) or die(mysql_error());
	echo "Success";
}

function delStock(){
	$id = $_POST["id"];

	$sql = "SELECT * FROM stock WHERE Barcode='".$id."';";
	$qry = mysql_query($sql);
	$row = mysql_fetch_assoc($qry);

	$sql = "INSERT INTO del_log(Barcode,Category,Product_Name,Price,Del_By,Del_Date) VALUES('".$row["Barcode"]."',".$row["Category"].",'".$row["Product_Name"]."',".$row["Price"].",'".$_SESSION["usernames"]."',now());";
	mysql_query($sql);

	$sql = "DELETE FROM stock WHERE Barcode='".$id."';";
	mysql_query($sql) or die(mysql_error());
	echo "Success";
}

function getPName(){
	$id = $_POST["id"];

	$sql = "SELECT Product_Name FROM stock WHERE Barcode='".$id."';";
	//echo $sql;
	$qry = mysql_query($sql);
	$row = mysql_fetch_assoc($qry);
	echo $row["Product_Name"];
}

function getAccess(){
	$id = $_REQUEST["id"];
	$sql = "SELECT * FROM pch_access WHERE user_id='".$id."';";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	$data = $row["access"];
?>
	<table width="500">
		<tr>
			<td>&nbsp;</td>
			<td>
				<table>
					<tr>
						<td width="50">Read</td>
						<td width="50">Edit</td>
						<td width="50">Delete</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>สั่งของ</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="a_read" <?=(substr($data,0,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>ของส่งซ่อมจากเกาะ</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="o_read" <?=(substr($data,14,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>รับของ</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="b_read" <?=(substr($data,1,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>ส่งของกลับฝั่ง</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="c_read" <?=(substr($data,2,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>จัดการสินค้า</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="d_read" <?=(substr($data,3,1)=="1")?"checked":""?>></td>
						<td width="50"><input type="checkbox" id="e_edit" <?=(substr($data,4,1)=="1")?"checked":""?>></td>
						<td width="50"><input type="checkbox" id="f_delete" <?=(substr($data,5,1)=="1")?"checked":""?>></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>ส่งของไปเกาะ</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="g_read" <?=(substr($data,6,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>รายการค้างส่ง</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="h_read" <?=(substr($data,7,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>รายการส่งของจากเกาะ</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="i_read" <?=(substr($data,8,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>รายการของส่งเกิน</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="j_read" <?=(substr($data,9,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>ประวัติการสั่งของ</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="k_read" <?=(substr($data,10,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>ประวัติการรับของ</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="l_read" <?=(substr($data,11,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>Access List</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="m_read" <?=(substr($data,12,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>User Manage</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="n_read" <?=(substr($data,13,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50">&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>ส่งของซ่อม</td>
			<td>
				<table>
					<tr>
						<td width="50"><input type="checkbox" id="p_read" <?=(substr($data,15,1)=="1")?"checked":""?>></td>
						<td width="50">&nbsp;</td>
						<td width="50"><input type="checkbox" id="q_delete" <?=(substr($data,16,1)=="1")?"checked":""?>></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<div><input type="button" value="Update" onclick="AUpdate()"></div>
<?
}

function updateAccess(){
	$id = $_REQUEST["id"];
	$data = $_REQUEST["data"];

	$sql = "SELECT * FROM pch_access WHERE user_id='".$id."';";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) > 0){
		$sql = "UPDATE pch_access SET access='".$data."' WHERE user_id='".$id."';";
		mysql_query($sql) or die(mysql_error());
		echo "Success";
	}else{
		$sql = "INSERT INTO pch_access(access,user_id) VALUES('".$data."','".$id."');";
		mysql_query($sql) or die(mysql_error());
		echo "Success";
	}
}

function updateOrCat(){
	$catid = $_REQUEST["catid"];
?>
	<span>Product : </span>
	<select id="or_item" onchange="updateOrName()">
		<option value="">&nbsp;</option>
		<?
			$sqlP = "SELECT * FROM pch_product_language join pch_product as a on a.id=product_id WHERE category_id='".$catid."' ORDER BY barcode;";
			$queryP = mysql_query($sqlP);
			while($rowP = mysql_fetch_array($queryP)){
		?>
			<option value="<?=$rowP["product_id"]?>"><?=$rowP["barcode"]?> | <?=$rowP["th"]?> | <?=$rowP["mm"]?></option>
		<?
			}
		?>
	</select>
<?
}

function updateSendItem(){
	$id = $_REQUEST["id"];
?>
	<select id="send_item" onchange="updateSendOName()">
		<option value="">&nbsp;</option>
		<?
			$sql = "SELECT * FROM product WHERE catid='".$id."';";
			$query = mysql_query($sql);
			while($row = mysql_fetch_array($query)){
			?>
				<option value="<?=$row["id"].":".$row["barcode"]?>"><?=$row["barcode"]." | ".$row["th"]." | ".$row["mm"]?></option>
			<?
			}
		?>
	</select>	
<?
}

function addSendOffice(){


	$item = $_POST["item"];
	$amount = $_POST["amount"];
	$unit = $_POST["unit"];
	$desc = $_POST["desc"];

	$last = 0;

	$sql = "INSERT INTO pch_sendoffice(sendby,sendDate) VALUES('".$_SESSION["usernames"]."',now());";
	$query = mysql_query($sql) or die(mysql_error());
	$last = mysql_insert_id();
	//echo $sql;
	for($i=0;$i<count($item);$i++){
		if($i == 0){
			$values .= "";
		}else{
			$values .= ",";
		}
		$values .= "(".$last.",'".$item[$i]."',".$amount[$i].",'".$unit[$i]."','".$desc[$i]."','".$_SESSION["usernames"]."',now())";
	}

	$sql = "INSERT INTO pch_sendoffice_detail(send_id,item,amount,unit,description,send_by,sendtime) VALUES".$values.";";
	mysql_query($sql) or die(mysql_error());
	//$last = mysql_insert_id();
	echo "ok:".$last;

}

function addSendIsland(){
	$item = $_POST["item"];
	$amount = $_POST["amount"];
	$unit = $_POST["unit"];
	$barcode = $_POST["barcode"];
	$desc = $_POST["desc"];

	$last = 0;

	$sql = "INSERT INTO pch_sendisland(sendby,sendDate,state) VALUES('".$_SESSION["usernames"]."',now(),'0');";
	echo $sql;
	$query = mysql_query($sql) or die(mysql_error());
	$last = mysql_insert_id();
	//echo $sql;
	for($i=0;$i<count($item);$i++){
		if($i == 0){
			$values .= "";
		}else{
			$values .= ",";
		}
		$values .= "(".$last.",'".$barcode[$i]."','".$item[$i]."',".$amount[$i].",'".$unit[$i]."','".$_SESSION["usernames"]."',now())";
	}

	$sql = "INSERT INTO pch_sendisland_detail(send_id,send_barcode,item,amount,unit,send_by,sendtime) VALUES".$values.";";
	echo $sql;
	mysql_query($sql) or die(mysql_error());
	$sql = "DELETE FROM pch_tmp_send;";
	mysql_query($sql) or die(mysql_error());
	//$last = mysql_insert_id();
	echo "ok:".$last;
}

function auto_checkAccept(){
	$sql = "select id from pch_order ORDER by id DESC limit 0,1";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	$order_id = $row["id"];

	$sql = "select id from pch_sendisland ORDER by id DESC limit 0,1";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	$send_id = $row["id"];

	$sql = "select * FROM pch_order_detail as tbA where order_id='".$order_id."' AND NOT EXISTS (select * FROM pch_sendisland_detail as tbB WHERE send_id='".$send_id."' AND tbA.product_id=tbB.item);";
	$query = mysql_query($sql);
	$i=0;
	$insert = "";
	$update = "";
	while($row = mysql_fetch_array($query)){
		$sqlx = "SELECT * FROM pch_unfinished WHERE item='".$row["product_id"]."';";
		$qry = mysql_query($sqlx);
		if(mysql_num_rows($qry) > 0){
			while($xrow = mysql_fetch_array($qry)){
				if($xrow["order_id"] != $order_id){
					$update = "UPDATE pch_unfinished SET amount = amount + ".$row["amount"]." WHERE id='".$xrow["id"]."';";
					mysql_query($update);
					if(mysql_error()){
						echo mysql_error()."\n".$sql;
					}
				}
			}
		}else{
			if($i > 0){
				$insert .= ",('".$row["order_id"]."','".$row["product_id"]."','".$row["amount"]."','".$row["unit"]."','".$row["last_update"]."')";
			}else{
				$insert = "('".$row["order_id"]."','".$row["product_id"]."','".$row["amount"]."','".$row["unit"]."','".$row["last_update"]."')";;
			}
		}
		$i++;
	}

	if($insert != ""){
		$inssql = "INSERT INTO pch_unfinished(order_id,item,amount,unit,orderTime) VALUES";
		$inssql .= $insert.";";
		mysql_query($inssql);
		if(mysql_error()){
			echo mysql_error()."\n".$inssql;
		}
	}
}

function acceptOrder(){
	$sendid = $_REQUEST["sendid"];
	$data = $_REQUEST["data"];
	$unknow_data = $_REQUEST["unknow_data"];
	$error = "";

	if($data != ""){
		auto_checkAccept();

		$sql = "SELECT * FROM pch_accept_order WHERE send_id='".$sendid."';";
		$query = mysql_query($sql);
		if(mysql_num_rows($query) > 0){

		}else{

			$tmp = explode(";",$data);

			if(count($tmp) > 0){
				$sql = "INSERT INTO pch_accept_order(send_id,checkDate,checkby) VALUES(".$sendid.",now(),'".$_SESSION["usernames"]."');";
				$query = mysql_query($sql) or die(mysql_error());
				$accept_id = mysql_insert_id();
			
				for($i=0;$i<count($tmp);$i++){
					$adata = explode(":",$tmp[$i]);
					$send_detail_id = $adata[0];
					$pro_id = $adata[1];
					$send_amount = $adata[2];
					$real_amount = $adata[3];
					
					if($i == 0){
						$values .= "";
					}else{
						$values .= ",";
					}
					$values .= "(".$accept_id.",".$sendid.",".$send_detail_id.",'".$pro_id."',".$send_amount.",".$real_amount.",'".$_SESSION["usernames"]."',now(),'0')";
				}

				$sql = "INSERT INTO pch_accept_order_detail(accept_id,send_id,send_detail_id,item,sendamount,realamount,confirmby,confirmDate,state) VALUES".$values.";";
				//echo $sql;
				mysql_query($sql);
				if(mysql_error()){
					$error .= "A:".$sql.":".mysql_error();
				}
			}
		}
	}

	$values = "";
	
	if($unknow_data != ""){

		$unknow_id = 0;

		$sql = "INSERT INTO pch_unknow_receive(send_id,confirmDate,confirmby) VALUES(".$sendid.",now(),'".$_SESSION["usernames"]."');";
		$query = mysql_query($sql) or die(mysql_error());
		$unknow_id = mysql_insert_id();

		$tmp = explode(";",$unknow_data);
		for($i=0;$i<count($tmp);$i++){
			$adata = explode(":",$tmp[$i]);
			$barcode = $adata[0];
			$product = $adata[1];
			$amount = $adata[2];
			$unit = $adata[3];
			
			if($i == 0){
				$values .= "";
			}else{
				$values .= ",";
			}
			$values .= "(".$unknow_id.",".$sendid.",'".$barcode."','".$product."',".$amount.",'".$unit."','".$_SESSION["usernames"]."',now())";
		}

		$sql = "INSERT INTO pch_unknow_receive_detail(unknow_id,send_id,barcode,product,amount,unit,confirmby,confirmDate) VALUES".$values.";";
		//echo $sql;
		mysql_query($sql);
		if(mysql_error()){
			$error .= "B:".$sql.":".mysql_error();
		}
	}
	
	$sql = "UPDATE pch_sendisland SET state='1' WHERE id='".$sendid."'";
	//echo $sql;
	mysql_query($sql);
	if(mysql_error()){
		$error .= "C:".$sql.":".mysql_error();
	}

	if($error != ""){
		echo $error;
	}else{
		echo "Success";
	}
}

function confirmSended(){
	$id = $_REQUEST["id"];
	$sql = "UPDATE pch_accept_order_detail SET state='1' WHERE id='".$id."';";
	mysql_query($sql);
	if(mysql_error()){
		$error .= mysql_error();
	}

	if($error != ""){
		echo $error;
	}else{
		echo "ok:";
	}
}

function searchProduct(){
	$search = $_REQUEST["search"];
	?>
	<table border="1" width="100%" style="table-layout:fixed;">
		<tr>
								
	<?
		if($search != ""){
			$where = " WHERE barcode like '%".$search."%' OR th like '%".$search."%';";
		}else{
			$where = ";";
		}

		$sql = "SELECT a.id AS ids,category_name,barcode,th,mm,image,unit_name,targetname,targetmm FROM (pch_product AS a right join pch_product_language on a.id = product_id) Inner Join pch_category AS b ON a.category_id = b.id Left Join pch_product_call AS c ON a.id = c.id_product Left Join pch_unit AS d ON c.id_unit = d.id left join pch_product_target as e on a.id=e.product_id left join pch_target as f on e.target_id=f.id".$where;
		$query = mysql_query($sql);
		$i=0;
		while($row = mysql_fetch_array($query) or die(mysql_error())){
		?>
				<td width="50%" id="tbl_product_<?=$row["ids"]?>">
					<table width="100%">
						<tr>
							<td style="width:120px;height:100px;" align="center"><?if($row["image"] != ""){ echo '<a href="'.$row["image"].'" target="_blank"><img src="'.$row["image"].'" width="100" style="border:0px;"></a>';}else{ echo "&nbsp;";}?></td>
							<td>
								<div>Barcode : <?=$row["barcode"]?></div>
								<div>ชื่อสินค้า : <?=$row["th"]?></div>
								<div>Product Name : <?=$row["mm"]?></div>
								<div>Category : <?=$row["category_name"]?></div>
								<div>Unit : <?=$row["unit_name"]?></div>
								<div>สถานที่เก็บ : <?=$row["targetname"]?></div>
								<div>
									<?
										if(checkAccess("4")){
									?>
										<a href="#" onclick="return frmEditProduct('<?=$row["ids"]?>');">Edit</a>
									<?
										}
									?>
									&nbsp;&nbsp;<a href="#" onclick="getOrderForm(<?=$row["ids"]?>)">Buy</a>
								</div>
							</td>
						</tr>				
					</table>
				</td>
		<?
			if($i%2){
				echo "</tr><tr>";
			}
			$i++;
		}
	?>
		</tr>
	</table>
	<?
}

function viewAcceptDetail(){
	$id = $_REQUEST["id"];
	
	$sql = "SELECT th,mm,amount,realamount,unit,sendtime FROM (pch_sendisland_detail as a join product as b on a.item=b.id) left join pch_accept_order_detail as c on a.send_id=c.send_id WHERE a.send_id='".$id."';";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) > 0){
		?>
		<div><a href="#" onclick="printNotAccept('<?=$id?>');">Print Preview</a></div><br>
		<table border="1" width="100%">
			<tr>
				<td>ชื่อสินค้า</td>
				<td>Product Name</td>
				<td>Send Amount</td>
				<td>Real Amount</td>
				<td>Unit</td>
				<td>Send Time</td>
			</tr>
		<?
		while($row = mysql_fetch_array($query)){
		?>
			<tr>
				<td><?=$row["th"]?></td>
				<td><?=$row["mm"]?></td>
				<td><?=$row["amount"]?></td>
				<td><?if($row["realamount"] == ""){echo $row["amount"];}else{ echo "<span style='color:red;font-weight:bold;'>".$row["realamount"]."</span>";}?></td>
				<td><?=$row["unit"]?></td>
				<td><?=$row["sendtime"]?></td>
			</tr>
		<?
		}
		?>
		</table>
		<input type="button" onclick="acceptOrder()" value="Back">
		<?
	}else{
		echo "No Data";
	}
}

function addOrderCart(){
	$or_item = $_REQUEST["or_item"];
	$or_amount = $_REQUEST["or_amount"];
	$or_itemname = $_REQUEST["or_itemname"];
	$or_unit = $_REQUEST["or_unit"];
	$or_unittext = $_REQUEST["or_unittext"];
	$or_quick = $_REQUEST["or_quick"];

	$sql = "INSERT INTO pch_tmp_order(item_id,item_name,amount,unit_id,unit_name,quick) VALUES('".$or_item."','".$or_itemname."','".$or_amount."','".$or_unit."','".$or_unittext."','".$or_quick."');";
	$query = mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}

}

function loadCart(){
	$sql = "SELECT id,item_id,item_name,amount,unit_id,unit_name,quick FROM pch_tmp_order ORDER BY id DESC;";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) > 0){
	?>
		<table id="or_lists" width="700" border="1">
		<tr>
			<td colspan="4">รายการสั่งของลงเกาะวันที่  <?=date("d/m/Y");?></td>
		</tr>
		<tr>
			<td width="550">Product Name</td>
			<td width="50" align="center">Amount</td>
			<td width="50" align="center">Unit</td>
			<td width="50">&nbsp;</td>
		</tr>
		<?
		while($row = mysql_fetch_array($query)){
			if($row["quick"] == "1"){
				$style='style="color:red"';
			}else{
				$style='';
			}
		?>
			<tr id="tr_<?=$row["id"]?>">
				<td <?=$style?>><input type="hidden" name="or_i[]" value="<?=$row["item_id"]?>"><?=$row["item_name"]?></td>
				<td><input type="hidden" name="or_a[]" value="<?=$row["amount"]?>"><?=$row["amount"]?></td>
				<td><input type="hidden" name="or_u[]" value="<?=$row["unit_id"]?>"><?=$row["unit_name"]?></td>
				<td><input type="hidden" name="or_q[]" value="<?=$row["quick"]?>"><a href="#" onclick="delOrItem(<?=$row["id"]?>);">del</a></td>
			</tr>
		<?
		}
		?>
		</table>
		<div id="checkOutBtn"><input type="button" id="btnCheckOut" onclick="orderCheckOut()" value="Check Out" style="width:100px;"></div>
	<?
	}
}

function removeCart(){
	$id = $_REQUEST["id"];
	$sql = "DELETE FROM pch_tmp_order WHERE id='".$id."';";
	mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}
}


function addSendCart(){
	$send_item = $_REQUEST["send_item"];
	$send_barcode = $_REQUEST["send_barcode"];
	$send_amount = $_REQUEST["send_amount"];
	$send_name = $_REQUEST["send_name"];
	$send_unit = $_REQUEST["send_unit"];
	$send_unittext = $_REQUEST["send_unittext"];
	
	$sql = "INSERT INTO pch_tmp_send(item_id,item_barcode,item_name,amount,unit_id,unit_name) VALUES('".$send_item."','".$send_barcode."','".$send_name."','".$send_amount."','".$send_unit."','".$send_unittext."');";
	$query = mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}
}

function updateSendCart(){
	$sql = "SELECT id,item_id,item_barcode,item_name,amount,unit_id,unit_name FROM pch_tmp_send ORDER BY id DESC;";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) > 0){
	?>
		<table id="send_lists" width="700" border="1">
		<tr>
			<td colspan="4">รายกาส่งของลงเกาะวันที่  <?=date("d/m/Y");?></td>
		</tr>
		<tr>
			<td>Barcode</td>
			<td width="550">Product Name</td>
			<td width="50" align="center">Amount</td>
			<td width="50" align="center">Unit</td>
			<td width="50">&nbsp;</td>
		</tr>
		<?
		while($row = mysql_fetch_array($query)){
			if($row["quick"] == "1"){
				$style='style="color:red"';
			}else{
				$style='';
			}
		?>
			<tr id="tr_<?=$row["id"]?>">
				<td><input type="hidden" name="send_b[]" value="<?=$row["item_barcode"]?>"><?=$row["item_barcode"]?></td>
				<td <?=$style?>><input type="hidden" name="send_i[]" value="<?=$row["item_id"]?>"><?=$row["item_name"]?></td>
				<td><input type="hidden" name="send_a[]" value="<?=$row["amount"]?>"><?=$row["amount"]?></td>
				<td><input type="hidden" name="send_u[]" value="<?=$row["unit_id"]?>"><?=$row["unit_name"]?></td>
				<td><input type="hidden" name="send_q[]" value="<?=$row["quick"]?>"><a href="#" onclick="delSendIslandItem(<?=$row["id"]?>);">del</a></td>
			</tr>
		<?
		}
		?>
		</table>
		<div id="checkOutBtn"><input type="button" id="btnSendCheckOut" onclick="sendIslandCheckOut()" value="Send" style="width:90px;"></div>
	<?
	}
}

function removeSendCart(){
	$id = $_REQUEST["id"];
	$sql = "DELETE FROM pch_tmp_send WHERE id='".$id."';";
	mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}
}

function clearUnfinished(){
	$id = $_REQUEST["id"];

	$sql = "UPDATE pch_accept_order_detail SET state='1' WHERE id='$id'";
	mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}
}

function delUnfinished(){
	$id = $_REQUEST["id"];

	$sql = "DELETE FROM pch_unfinished WHERE id='$id'";
	echo $sql;
	mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}
}

function getOrderForm(){
	$item = $_REQUEST["or_item"];

	$sql = "SELECT * FROM pch_product where id='".$item."';";
	$query = mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	?>
		<input type="hidden" id="or_item" value="<?=$item?>">
		<table>
			<tr>
				<td align="right">Product : </td>
				<td align="left"><input type="hidden" id="or_itemname" value="<?=$row["product_name"]?>"><?=$row["product_name"]?></td>
			</tr>
			<tr>
				<td align="right">Amount : </td>
				<td align="left"><input type="text" id="or_amount"></td>
			</tr>	
			<tr>
				<td align="right">Unit : </td>
				<td align="left">
					<select id="or_unit" onchange="updateOrUnit()">
						<?
							$sqlPC = "SELECT * FROM pch_product_call WHERE id_product='".$item."';";

							$qryPC = mysql_query($sqlPC);
							$rowPC = mysql_fetch_assoc($qryPC);
							$sqlP = "SELECT * FROM pch_unit ORDER BY unit_name;";
							$queryP = mysql_query($sqlP);
							while($rowP = mysql_fetch_array($queryP)){
								if($rowPC["id_unit"] == $rowP["id"]){
									$sel = "selected";
									$text = $rowP["unit_name"];
								}else{
									$sel = "";
									$text = "";
								}
						?>
							<option value="<?=$rowP["id"]?>" <?=$sel?>><?=$rowP["unit_name"]?></option>
						<?
							}
						?>
					</select>
					<script>
						 updateOrUnit();
					</script>
					<input type="hidden" id="or_unittext" value="">
				</td>
			</tr>
			<tr>
				<td align="right">Quick : </td>
				<td align="left"><input type="checkbox" id="quick"></td>
			</tr>
			<tr>
				<td colspan="2" align="center"></td>
			</tr>
		</table>		
			
	<?
}

function delUser(){
	$id = $_REQUEST["id"];
	$sql = "DELETE FROM member WHERE id='".$id."';";
	$query = mysql_query($sql);

}

function newRepair(){
?>
<input type="hidden" id="idRow" value="0">
<table>
	<tr>
		<td>Product</td>
		<td><input type="text" id="product"></td>
	</tr>
	<tr>
		<td>Qty</td>
		<td><input type="text" id="qty"></td>
	</tr>
	<tr>
		<td>Detail</td>
		<td><input type="text" id="detail"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="button" value="Add" onclick="insertRepair()"></td>
	</tr>
</table><hr>
<div id="showNewRepair" style="display:none;">
	<table id="repairTable" width="100%">
		<thead>
			<tr>
				<td width="40%">Product</td>
				<td width="10%" align="center">Qty</td>
				<td width="40%">Detail</td>
				<td width="10%">&nbsp;</td>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	<input type="button" onclick="addRepair()" value="Save">
</div>
<?
}

function addRepair(){

	$myArray = json_decode(stripslashes($_POST['myArray']), true);
	//print_r($myArray);

	$sql = "INSERT INTO pch_repair_send(product,qty,detail,send_date,last_update) VALUES";
	$i = 0;

	foreach($myArray as $data){
		if($i > 0){
			$sql .= ",";
		}
		
		
		$sql .=  "('".$data["product"]."','".$data["qty"]."','".$data["detail"]."',now(),now())";
		
		$i++;
	}
	$sql .=";";
	mysql_query($sql);

	if(mysql_error()){
		echo $sql;
	}else{
		echo "OK";
	}
	
}

function confirmRepair(){
	$id = $_REQUEST["id"];

	$sql = "UPDATE pch_repair_send SET confirm='".$_SESSION["usernames"]."',confirm_date=now() WHERE id='".$id."';";
	mysql_query($sql);
	if(mysql_error()){
		echo $sql;
	}else{
		echo "OK";
	}
}

function addStockCart(){
	$send_item = $_REQUEST["send_item"];
	$send_barcode = $_REQUEST["send_barcode"];
	$send_amount = $_REQUEST["send_amount"];
	$send_name = $_REQUEST["send_name"];
	$send_unit = $_REQUEST["send_unit"];
	$send_unittext = $_REQUEST["send_unittext"];
	$expired = $_REQUEST["expired"];

	
	$sql = "INSERT INTO pch_tmp_stock(item_id,item_barcode,item_name,amount,unit_id,unit_name,expired) VALUES('".$send_item."','".$send_barcode."','".$send_name."','".$send_amount."','".$send_unit."','".$send_unittext."','".$expired."');";
	$query = mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}
}

function updateStockCart(){
	$sql = "SELECT id,item_id,item_barcode,item_name,amount,unit_id,unit_name,expired FROM pch_tmp_stock ORDER BY id DESC;";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) > 0){
	?>
		<table id="send_lists" width="700" border="1">
		<tr>
			<td colspan="4">รายการซื้อของเข้า Stock  <?=date("d/m/Y");?></td>
		</tr>
		<tr>
			<td>Barcode</td>
			<td width="550">Product Name</td>
			<td width="50" align="center">Amount</td>
			<td width="50" align="center">Unit</td>
			<td width="50" align="center">Expired</td>
			<td width="50">&nbsp;</td>
		</tr>
		<?
		while($row = mysql_fetch_array($query)){
			if($row["quick"] == "1"){
				$style='style="color:red"';
			}else{
				$style='';
			}
		?>
			<tr id="tr_<?=$row["id"]?>">
				<td><input type="hidden" name="send_b[]" value="<?=$row["item_barcode"]?>"><?=$row["item_barcode"]?></td>
				<td <?=$style?>><input type="hidden" name="send_i[]" value="<?=$row["item_id"]?>"><?=$row["item_name"]?></td>
				<td><input type="hidden" name="send_a[]" value="<?=$row["amount"]?>"><?=$row["amount"]?></td>
				<td><input type="hidden" name="send_u[]" value="<?=$row["unit_id"]?>"><?=$row["unit_name"]?></td>
				<td><input type="hidden" name="send_exp[]" value="<?=$row["expired"]?>"><?=$row["expired"]?></td>
				<td><input type="hidden" name="send_q[]" value="<?=$row["quick"]?>"><a href="#" onclick="delStockOfficeItem(<?=$row["id"]?>);">del</a></td>
			</tr>
		<?
		}
		?>
		</table>
		<div id="checkOutBtn"><input type="button" id="btnSendCheckOut" onclick="stockOfficeCheckOut()" value="Save" style="width:90px;"></div>
	<?
	}
}

function removeStockCart(){
	$id = $_REQUEST["id"];
	$sql = "DELETE FROM pch_tmp_stock WHERE id='".$id."';";
	mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}
}

function addStockOffice(){
	$item = $_POST["item"];
	$amount = $_POST["amount"];
	$unit = $_POST["unit"];
	$barcode = $_POST["barcode"];
	$desc = $_POST["desc"];
	$exp = $_POST["exp"];

	$last = 0;

	$sql = "INSERT INTO pch_buystockoffice(sendby,sendDate) VALUES('".$_SESSION["usernames"]."',now());";
	//echo $sql;
	$query = mysql_query($sql) or die(mysql_error());
	$last = mysql_insert_id();
	//echo $sql;
	for($i=0;$i<count($item);$i++){
		if($i == 0){
			$values .= "";
			$values_Stock .= "";
		}else{
			$values .= ",";
			$values_Stock .= ",";
		}

		$values .= "(".$last.",'".$barcode[$i]."','".$item[$i]."',".$amount[$i].",'".$unit[$i]."','".$exp[$i]."','".$_SESSION["usernames"]."',now())";
		
		$sql = "SELECT * FROM pch_stock_office WHERE item_id=".$item[$i]." AND unit=".$unit[$i]." AND expired_date='".$exp[$i]."';";
		//echo $sql;
		$qry = mysql_query($sql);
		if(mysql_num_rows($qry) > 0){
			$sql = "UPDATE pch_stock_office SET qty = qty+".$amount[$i]." WHERE item_id='".$item[$i]."' AND unit='".$unit[$i]."' AND expired_date='".$exp[$i]."'; ";
			//echo $sql;
			mysql_query($sql) or die(mysql_error());
		}else{
			$sql = "INSERT INTO pch_stock_office(item_id,barcode,qty,unit,expired_date,last_update) VALUES('".$item[$i]."','".$barcode[$i]."','".$amount[$i]."','".$unit[$i]."','".$exp[$i]."',now());";
			//echo $sql;
			mysql_query($sql) or die(mysql_error());
		}


	}

	$sql = "INSERT INTO pch_buystockoffice_detail(send_id,send_barcode,item,amount,unit,expired,send_by,sendtime) VALUES".$values.";";
	//echo $sql;
	mysql_query($sql) or die(mysql_error());


	
	
	

	$sql = "DELETE FROM pch_tmp_stock;";
	mysql_query($sql) or die(mysql_error());
	//$last = mysql_insert_id();
	echo "ok:".$last;
}

function showRemoveStock(){
	$id = $_REQUEST["id"];
?>
	<select id="rem_item" onchange="updateRemOffice()">
		<option value="">&nbsp;</option>
		<?
			$sql = "select item_id as id,a.barcode as barcode,product_name,expired_date,c.id as uid,unit_name from (pch_stock_office as a join pch_product as b on a.item_id=b.id) join pch_unit as c on a.unit=c.id WHERE category_id='".$id."';";
			$query = mysql_query($sql);
			while($row = mysql_fetch_array($query)){
			?>
				<option value="<?=$row["id"].":".$row["uid"].":".$row["unit_name"].":".$row["product_name"].":".$row["expired_date"].":".$row["barcode"]?>"><?=$row["barcode"]." | ".$row["product_name"]." | ".$row["expired_date"]." | ".$row["unit_name"]?></option>
			<?
			}
		?>
	</select>	
<?
}


function remStockOfficeCart(){

	$rem_id = $_POST["rem_id"];
	$rem_name = $_POST["rem_name"];
	$rem_amount = $_POST["rem_amount"];
	$rem_uname = $_POST["rem_uname"];
	$rem_uid = $_POST["rem_uid"];
	$rem_barcode = $_POST["rem_barcode"];
	$rem_expired = $_POST["rem_expired"];



	$sql = "INSERT INTO pch_tmp_remstock(item_id,item_barcode,item_name,amount,unit_id,unit_name,expired) VALUES('".$rem_id."','".$rem_barcode."','".$rem_name."','".$rem_amount."','".$rem_uid."','".$rem_uname."','".$rem_expired."');";
	$query = mysql_query($sql);
	if(mysql_error()){
		echo mysql_error()."\n".$sql;
	}else{
		echo "ok:";
	}
}

function updateRemStockCart(){
	$sql = "SELECT id,item_id,item_barcode,item_name,amount,unit_id,unit_name,expired FROM pch_tmp_remstock ORDER BY id DESC;";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) > 0){
	?>
		<table id="send_lists" width="700" border="1">
		<tr>
			<td colspan="4">รายการเอาของออกจาก Stock  <?=date("d/m/Y");?></td>
		</tr>
		<tr>
			<td>Barcode</td>
			<td width="550">Product Name</td>
			<td width="50" align="center">Amount</td>
			<td width="50" align="center">Unit</td>
			<td width="50">&nbsp;</td>
		</tr>
		<?
		while($row = mysql_fetch_array($query)){
			if($row["quick"] == "1"){
				$style='style="color:red"';
			}else{
				$style='';
			}
		?>
			<tr id="tr_<?=$row["id"]?>">
				<td><input type="hidden" name="send_b[]" value="<?=$row["item_barcode"]?>"><?=$row["item_barcode"]?></td>
				<td <?=$style?>><input type="hidden" name="send_i[]" value="<?=$row["item_id"]?>"><?=$row["item_id"].":".$row["item_name"]?></td>
				<td><input type="hidden" name="send_a[]" value="<?=$row["amount"]?>"><?=$row["amount"]?></td>
				<td><input type="hidden" name="send_u[]" value="<?=$row["unit_id"]?>"><?=$row["unit_name"]?></td>
				<td><input type="hidden" name="send_exp[]" value="<?=$row["expired"]?>"><a href="#" onclick="delRemStockOfficeItem('<?=$row["id"]?>');">del</a></td>
			</tr>
		<?
		}
		?>
		</table>
		<div id="checkOutBtn"><input type="button" id="btnSendCheckOut" onclick="saveStockRemove()" value="Save" style="width:90px;"></div>
	<?
	}
}

function remFromRemStockOfficeList(){
	$id = $_REQUEST["id"];

	$sql = "DELETE FROM pch_tmp_remstock WHERE id='".$id."';";
	mysql_query($sql);

}

function remStockOffice(){

	$item = $_POST["item"];
	$amount = $_POST["amount"];
	$unit = $_POST["unit"];
	$barcode = $_POST["barcode"];
	$exp = $_POST["exp"];
	
	$last = 0;

	$sql = "INSERT INTO pch_remstockoffice(sendby,sendDate) VALUES('".$_SESSION["usernames"]."',now());";
	echo $sql;
	$query = mysql_query($sql) or die(mysql_error());
	$last = mysql_insert_id();
	//echo $sql;
	
	for($i=0;$i<count($item);$i++){

			if($i == 0){
				$values .= "";
				$values_Stock .= "";
			}else{
				$values .= ",";
				$values_Stock .= ",";
			}

			$values .= "(".$last.",'".$barcode[$i]."','".$item[$i]."',".$amount[$i].",'".$unit[$i]."','".$exp[$i]."','".$_SESSION["usernames"]."',now())";

			$sql = "UPDATE pch_stock_office SET qty = qty-".$amount[$i]." WHERE item_id='".$item[$i]."' AND unit='".$unit[$i]."' AND expired_date='".$exp[$i]."'; ";
			//echo $sql;
			mysql_query($sql) or die(mysql_error());
			
	}

	$sql = "INSERT INTO pch_remstockoffice_detail(send_id,send_barcode,item,amount,unit,expired,send_by,sendtime) VALUES".$values.";";
	//echo $sql;
	mysql_query($sql) or die(mysql_error());

	$sql = "DELETE FROM pch_stock_office WHERE qty < 0;";
	//echo $sql;
	mysql_query($sql) or die(mysql_error());

	$sql = "DELETE FROM pch_tmp_remstock;";
	mysql_query($sql) or die(mysql_error());
	//$last = mysql_insert_id();
	echo "ok:".$last;
}

function repairSaveNew(){
	$product = $_REQUEST["product"];
	$senddate = $_REQUEST["senddate"];
	$place = $_REQUEST["place"];
	$billno = $_REQUEST["billno"];
	$receivedate = $_REQUEST["receivedate"];
	$sendby = $_REQUEST["sendby"];

	$sql = "INSERT INTO pch_repair_item(product_name,send_date,send_place,bill_no,receive_date,send_by,create_date) VALUES('".$product."','".$senddate."','".$place."','".$billno."','".$receivedate."','".$sendby."',now());";
	mysql_query($sql) or die(mysql_error());
	echo "OK:";
}

function finishRepairAdd(){
	$id = $_REQUEST["id"];

	$sql = "UPDATE pch_repair_item SET accept='Y' WHERE id='".$id."';";
	mysql_query($sql) or die(mysql_error());
	echo "OK:";
}

function delRepairAdd(){
	$id = $_REQUEST["id"];

	$sql = "UPDATE pch_repair_item SET accept='D' WHERE id='".$id."';";
	mysql_query($sql) or die(mysql_error());
	echo "OK:";
}

function addExport(){
	$orderid = $_REQUEST["orderid"];
	$orderdate = $_REQUEST["orderdate"];
	$productid = $_REQUEST["productid"];
	$product = $_REQUEST["product"];
	$barcode = $_REQUEST["barcode"];
	$amount = $_REQUEST["amount"];
	$unit = $_REQUEST["unit"];
	$dealer = $_REQUEST["dealer"];
	$orderby = $_REQUEST["orderby"];

	$sql = "INSERT INTO pch_orderexport(order_id,orderdate,status,place,product_id,barcode,product_name,amount,unit,order_by,last_update) VALUES('".$orderid."','".$orderdate."','0','".$dealer."','".$productid."','".$barcode."','".$product."','".$amount."','".$unit."','".$orderby."',now());";
	//echo $sql;
	$query = mysql_query($sql);
	$export_id = mysql_insert_id();
	if(mysql_error()){
		echo mysql_error();
	}else{
		echo "Saved";
	}
}

function finishExport(){
	$id = $_REQUEST["id"];
	$sql = "UPDATE pch_orderexport SET status='1',last_update=now() WHERE order_id='".$id."';";
	$query = mysql_query($sql);
	if(mysql_error()){
		echo mysql_error();
	}else{
		echo "Saved";
	}
}

function saveEditDealer(){
	$id = $_REQUEST["id"];
	$name = $_REQUEST["name"];
	$tel = $_REQUEST["tel"];
	$fax = $_REQUEST["fax"];

	$sql = "UPDATE pch_dealer SET dealer='".$name."',tel='".$tel."',fax='".$fax."' WHERE id='".$id."';";
	$query = mysql_query($sql);
	if(mysql_error()){
		echo mysql_error();
	}else{
		echo "Saved";
	}
}

function delDealer(){
	$id = $_REQUEST["id"];
	$sql = "DELETE FROM pch_dealer WHERE id='".$id."';";
	$query = mysql_query($sql);
	if(mysql_error()){
		echo mysql_error();
	}else{
		echo "Saved";
	}
}

function saveNewDealer(){
	$name = $_REQUEST["name"];
	$tel = $_REQUEST["tel"];
	$fax = $_REQUEST["fax"];

	$sql = "INSERT INTO pch_dealer(dealer,tel,fax) VALUES('".$name."','".$tel."','".$fax."');";
	$query = mysql_query($sql);
	if(mysql_error()){
		echo mysql_error();
	}else{
		echo "Saved";
	}
}
?>